import torch
a=torch.zeros(2,3)
print(f'全零张量2x3{a}')
b=torch.ones(2,3)
print(f'全一张量2x3{b}')
c=torch.randn(2,3)
print(f'随机张量2x3{c}')
import numpy as np
numpy_array=np.array([[1,2],[3,4]])
tensor_from_numpy=torch.from_numpy(numpy_array)
print(f'numpy矩阵{numpy_array}')
print(f'numpy转化的张量{tensor_from_numpy}')
device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
d =torch.randn(2,3,device=device)
print(d)
print(f'张量相加{a+b}')
print(f'张量点乘{a*b}')
print(f'张量转置{c.t()}')
print(f'return shape{c.shape}')
#创建一个需要梯度的张量
tensor_requires_grad=torch.tensor([1.0],dtype=torch.float32,requires_grad=True)
tensor_result=tensor_requires_grad*2
tensor_result.backward()
print(f'输出梯度{tensor_requires_grad.grad}')#输出梯度
